﻿using EnumsNET;

using MergeExcel.Common;
using MergeExcel.Models;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MergeExcel.Forms {

    public partial class FrmSameMerge : Form {

        public FrmSameMerge()
        {
            InitializeComponent();
        }
        private DataTable _result;
        #region 窗口载入

        private void FrmSameMerge_Shown(object sender, EventArgs e)
        {
            // 初始查找类型列表
            cbxFindType.Items.Clear();
            Enums.GetNames<E_Operator>()?.ToList()?.ForEach(itm => cbxFindType.Items.Add(itm));
            _result = new DataTable();
            _result.Columns.Add("姓名");
            _result.Columns.Add("电话");
            var row = _result.NewRow();
            row[0] = "森林森林森林森林森林森林森林森林";
            row[1] = "13188888888";
            _result.Rows.Add(row);
            ShowResult();
            SetConfig();
        }

        private void SetConfig()
        {
            tbxTableAKeywordIndex.Text = Comm.Cfg.MergeCfg.TableAKeywordIndex.ToString();
            tbxTableBKeywordIndex.Text = Comm.Cfg.MergeCfg.TableBKeywordIndex.ToString();
            if (cbxFindType.Items.Count > 0)
                cbxFindType.Text = Comm.Cfg.MergeCfg.FindType.GetName();
            rbAppendAll.Checked = rbAppend.Checked = rbOverwrite.Checked = false;
            if (Comm.Cfg.MergeCfg.ResultType == 0)
                rbAppendAll.Checked = true;
            else if (Comm.Cfg.MergeCfg.ResultType == 1)
                rbAppend.Checked = true;
            else if (Comm.Cfg.MergeCfg.ResultType == 2)
                rbOverwrite.Checked = true;
            else
            {
                Comm.Cfg.MergeCfg.ResultType = 0;
                rbAppendAll.Checked = true;
            }
            tbxAppendIndexs.Text = Comm.Cfg.MergeCfg.AppendIndexString;
            tbxTableAOverwriteIndexs.Text = Comm.Cfg.MergeCfg.TableAOverwriteIndexString;
            tbxTableBOverwriteIndexs.Text = Comm.Cfg.MergeCfg.TableBOverwriteIndexString;

        }

        #endregion 窗口载入
        #region 显示数据
        private void ShowResult()
        {
            dgvResult.DataSource = null;
            dgvResult.Rows?.Clear();
            dgvResult.Columns?.Clear();
            dgvResult.DataSource = _result;
            for (int i = 0; i < dgvResult.Columns.Count; i++)
            {
                dgvResult.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                dgvResult.Columns[i].DividerWidth = 1;
                dgvResult.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }
        #endregion
        #region 保存配置
        private void SaveConfig()
        {
            Comm.Cfg.MergeCfg.TableAKeywordIndex = tbxTableAKeywordIndex.Text.ToInt32();
            Comm.Cfg.MergeCfg.TableBKeywordIndex = tbxTableBKeywordIndex.Text.ToInt32();
            Comm.Cfg.MergeCfg.AppendIndexString = tbxAppendIndexs.Text;
            Comm.Cfg.MergeCfg.TableAOverwriteIndexString = tbxTableAOverwriteIndexs.Text;
            Comm.Cfg.MergeCfg.TableBOverwriteIndexString = tbxTableBOverwriteIndexs.Text;
            if (rbAppendAll.Checked)
                Comm.Cfg.MergeCfg.ResultType = 0;
            else if (rbAppend.Checked)
                Comm.Cfg.MergeCfg.ResultType = 1;
            else if (rbOverwrite.Checked)
                Comm.Cfg.MergeCfg.ResultType = 2;
            else
                Comm.Cfg.MergeCfg.ResultType = 0;
            Comm.Cfg.MergeCfg.FindType = Enums.Parse<E_Operator>(cbxFindType.Text);
            Comm.SaveConfig();
        }
        #endregion
        #region 显示帮助
        private void PcHelp_Click(object sender, EventArgs e)
        {
            FrmFindTypeHelp f = new FrmFindTypeHelp();
            f.ShowDialog();
        }
        #endregion
        #region 开始对比
        private void BtnStart_Click(object sender, EventArgs e)
        {

        }
        #endregion
        #region 输出结果
        private async void BtnSaveResult_Click(object sender, EventArgs e)
        {
            try
            {
                if(_result.Rows.Count < 1)
                    throw new Exception("没有数据");
                string fileName = Comm.OutputFileName();
                if(fileName.Empty())
                    throw new Exception("必须选择输出文件");
                Comm.EF(false);
                var err = await Task.Run(() =>
                {
                    try
                    {

                        return null;
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                });
                if(!err.Empty())
                    throw new Exception(err);
                MessageBox.Show("OK");
                Comm.EF(true);
            }
            catch (Exception ex)
            {
                Comm.EF(true);
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}